// pages/login/login.js
var app = getApp();
const util = require('../../utils/util.js')
Page({

  /**
   * 页面的初始数据
   */
  data: {
    userInfo: '',
    passwordType:'password',
    canLogin:false,
    formData:{
      phone:'',
      password:'',
      myLongitude:'',
      myLatitude: '',
      error: ''
    }
  },

  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: function (options) {
    //wx.clearStorageSync()
    this.getIs()
  },
  getIs: function () {
    var _this = this;
    wx.request({
      url: app.globalData.httpApiCar + 'menu/index',
      data: {},
      header: {
        'content-type': 'application/json' // 默认值
      },
      success: function (res) {
        if (res.data.success) {
          _this.setData({
            error: res.data.error
          })
          if (res.data.error == 1) {
            if (wx.getStorageSync('sessionId')) {
              if (wx.getStorageSync('userInfo') != '') {
                wx.redirectTo({
                  url: '/pages/brandBusiness/brandBusiness',
                })
                this.setData({
                  ['formData.myLongitude']: wx.getStorageSync('location').longitude,
                  ['formData.myLatitude']: wx.getStorageSync('location').latitude,
                  userInfo: JSON.parse(wx.getStorageSync('userInfo'))
                })
                app.globalData.userInfo = JSON.parse(wx.getStorageSync('userInfo'))
              }

            }
          }
          else {
            if (wx.getStorageSync('sessionId')) {
              if (wx.getStorageSync('userInfo') != '') {
                wx.redirectTo({
                  url: '/pages/home/home',
                })
                this.setData({
                  ['formData.myLongitude']: wx.getStorageSync('location').longitude,
                  ['formData.myLatitude']: wx.getStorageSync('location').latitude,
                  userInfo: JSON.parse(wx.getStorageSync('userInfo'))
                })
                app.globalData.userInfo = JSON.parse(wx.getStorageSync('userInfo'))
              }

            }
          }
        }
      }
    })
  },
  // 显示明文密码
  showPad: function (e) {
    if (this.data.passwordType =='password'){
      this.setData({
        passwordType: 'text'
      })
    }else{
      this.setData({
        passwordType: 'password'
      })
    }
  },
  // 输入框值改变事件
  changeInput:function(e){
    var key = 'formData.' + e.target.dataset.name;
    this.setData({
      [key]:e.detail.value
    });
    var flag=false;
    // for (var j in this.data.formData) {
    //   if(this.data.formData[j]==''){
    //     flag=true;
    //   }
    //   else{
    //     flag = false;
    //   }
    // }
    if (this.data.formData.phone != '' && this.data.formData.password != ''){
      this.setData({
        canLogin: true
      });
    }else{
      this.setData({
        canLogin: false
      });
    }
  },
  // 登录
  formSubmit: function(){
    if (this.data.canLogin){
      var _this = this;
      wx.request({
        url: app.globalData.httpApiCar + 'tGyUserInfo/doLogin',
        data: _this.data.formData,
        header: {
          'content-type': 'application/json' // 默认值
        },
        success: function (res) {
          if (res.data.success) {
            if (_this.data.error != 1) {
              wx.redirectTo({
                url: '/pages/home/home',
              })
            }
            else{
              wx.redirectTo({
                url: '/pages/brandBusiness/brandBusiness',
              })
            }
            app.globalData.userInfo = res.data.data;
            wx.setStorageSync('userInfo', JSON.stringify(res.data.data))
          }
          else {
            wx.showToast({
              title: res.data.error,
              icon: 'none',
              duration: 2000
            })
          }
        },
        fail: function (res) {
          wx.showToast({
            title: res.data.error,
          })
        }
      })
    }
  },
  /**
   * 生命周期函数--监听页面初次渲染完成
   */
  onReady: function () {
  
  },

  /**
   * 生命周期函数--监听页面显示
   */
  onShow: function () {
  
  },

  /**
   * 生命周期函数--监听页面隐藏
   */
  onHide: function () {
  
  },

  /**
   * 生命周期函数--监听页面卸载
   */
  onUnload: function () {
  
  },

  /**
   * 页面相关事件处理函数--监听用户下拉动作
   */
  onPullDownRefresh: function () {
  
  },

  /**
   * 页面上拉触底事件的处理函数
   */
  onReachBottom: function () {
  
  },

  /**
   * 用户点击右上角分享
   */
  onShareAppMessage: function () {
  
  }
})